#include "stdafx.h"

#include "IGameState.h"
#include "IComponentSystem.h"

using namespace std;

namespace ZeEngine
{
    IGameState::IGameState()
    {
    }

    bool IGameState::InitSystems(GameEngine& gameEngine)
    {
        bool fSuccess = true;

        for (auto& pSystem : m_systems)
        {
            fSuccess &= pSystem->Init(gameEngine);
        }

        return fSuccess;
    }

    void IGameState::PreUpdateSystems(GameEngine& gameEngine)
    {
        for (auto& pSystem : m_systems)
        {
            pSystem->PreUpdate(gameEngine);
        }
    }

    bool IGameState::UpdateSystems(GameEngine& gameEngine)
    { 
        bool fQuit = false;

        for (auto& pSystem : m_systems)
        {
            fQuit |= pSystem->Update(gameEngine);
        }

        return fQuit;
    }

    void IGameState::PostUpdateSystems(GameEngine& gameEngine)
    {
        for (auto& pSystem : m_systems)
        {
            pSystem->PostUpdate(gameEngine);
        }
    }

    void IGameState::DestroySystems(GameEngine& gameEngine)
    {
        for (auto& pSystem : m_systems)
        {
            pSystem->Destroy(gameEngine);
        }

        m_systems.clear();
    }

    void IGameState::RenderSystems(SDL_Renderer& renderer, GameEngine& gameEngine, float interpolation)
    {
        for (auto& pSystem : m_systems)
        {
            pSystem->Render(renderer, gameEngine, interpolation);
        }
    }

    IComponentSystem* IGameState::AddSystem(unique_ptr<IComponentSystem> pSystemToAdd)
    {
        if (pSystemToAdd)
        {
            m_systems.push_back(move(pSystemToAdd));
        }

        return pSystemToAdd.get();
    }
}